package com.bolt.admin.module.sys.controller;

import com.bolt.admin.logging.annotation.LogAnnotation;
import com.bolt.admin.module.sys.dto.PrivilegeDTO;
import com.bolt.admin.module.sys.dto.PrivilegeQueryCriteria;
import com.bolt.admin.module.sys.entity.PrivilegeEntity;
import com.bolt.admin.module.sys.service.PrivilegeService;
import com.bolt.admin.module.sys.service.converter.PrivilegeConverter;
import com.bolt.common.collection.CollectionUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.support.ui.tree.helper.TreeRenderHelper;
import com.bolt.support.ui.tree.vo.UITreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "权限资源管理")
@RequestMapping("/api/sys/privilege")
@RequiredArgsConstructor
public class PrivilegeController {

    private final PrivilegeService privilegeService;


    @ApiOperation("查询部门列表")
    @GetMapping(value = "/page")
    public ResultMessage query(PrivilegeQueryCriteria queryCriteria) throws Exception {
        List <PrivilegeEntity> data;
        if (Objects.isNull(queryCriteria.getPid())) {
            data = this.privilegeService.findRoot();
        } else {
            data = privilegeService.queryPrivilege(queryCriteria);
        }
        return Results.success(PrivilegeConverter.toDTO(data));
    }

    @GetMapping(value = "getTree")
    public ResultMessage<List <UITreeNode>> getTree(@RequestParam(name = "pid", required = false) String id,
                                                    @RequestParam Map <String, String> params) {
        boolean loadAllNodes = !StrUtil.isEmpty(params.get("loadAllNodes")) && Boolean.parseBoolean(params.get("loadAllNodes"));
        List <UITreeNode> dataArray = new ArrayList <>();
        TreeRenderHelper.buildNodesTreeView(privilegeService, id, params, dataArray, loadAllNodes);
        return Results.success(dataArray);
    }

    @ApiOperation("权限资源管理:根据ID获取同级与上级数据")
    @PostMapping("/findAll")
    public ResultMessage<List <UITreeNode>> findALl(@RequestBody List <String> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            List <PrivilegeEntity> privilegeEntities = privilegeService.findAll(ids);
            List<UITreeNode> treeNodes = privilegeService.buildTree(privilegeEntities);
            return Results.success(treeNodes);
        }
        return Results.success(privilegeService.buildTree(privilegeService.findRoot()));
    }

    @ApiOperation("根据菜单ID返回所有子节点ID，包含自身ID")
    @GetMapping(value = "/childIds")
    public ResultMessage<Set <String>> getChildIds(String id) {
        List <PrivilegeEntity> menuEntities = new ArrayList <>();
        List <PrivilegeEntity> menuList;
        if (id != null) {
            menuList = privilegeService.findChildren(id);
        } else {
            menuList = privilegeService.findRoot();
        }
        menuEntities.add(privilegeService.findOne(id).get());
        menuEntities = privilegeService.getChildMenus(menuList, menuEntities);
        Set <String> ids = menuEntities.stream().map(PrivilegeEntity::getId).collect(Collectors.toSet());
        return Results.success(ids);
    }

    @ApiOperation("权限资源新增修改操作")
    @PostMapping
    @LogAnnotation(module = "权限资源管理", description = "权限资源新增操作")
    public ResultMessage save(@Validated @RequestBody PrivilegeDTO orgDTO) {

        privilegeService.saveOrUpdate(orgDTO);
        return Results.success();
    }

    @ApiOperation("权限资源新增修改操作")
    @PutMapping
    @LogAnnotation(module = "权限资源管理", description = "权限资源修改操作")
    public ResultMessage update(@Validated(PrivilegeDTO.Update.class) @RequestBody PrivilegeDTO orgDTO) {
        privilegeService.saveOrUpdate(orgDTO);
        return Results.success();
    }

    @ApiOperation("删除权限资源")
    @DeleteMapping
    @LogAnnotation(module = "权限资源管理", description = "删除权限资源")
    public ResultMessage delete(@RequestBody Set <String> ids) {
        List <PrivilegeEntity> allPrivilegeEntities = new ArrayList <>();
        for (String id : ids) {
            List <PrivilegeEntity> privilegeEntities = privilegeService.findChildren(id);
            allPrivilegeEntities.add(privilegeService.findOne(id).orElse(new PrivilegeEntity()));
            allPrivilegeEntities = privilegeService.findAllChildren(privilegeEntities, allPrivilegeEntities);
        }
        privilegeService.delete(allPrivilegeEntities);
        return Results.success();
    }

}

 